<?php

namespace app\admin\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Loader;
use think\Config;

class UpdateMenuComments extends Command
{
    // 中文菜单标题到语言键的映射
    protected $menuLangKeys = [
        '进销存管理' => 'KUERP_MANAGEMENT',
        '数据面板' => 'DATA_DASHBOARD',
        '打印' => 'PRINT',
        '购货2' => 'PURCHASE',
        '购货订单' => 'PURCHASE_ORDER',
        '查看' => 'VIEW',
        '添加' => 'ADD',
        '编辑' => 'EDIT',
        '删除' => 'DELETE',
        '审核/反审核' => 'APPROVE_UNAPPROVE',
        '合并入库' => 'MERGE_STOCK_IN',
        '内置函数' => 'INTERNAL_FUNCTION',
        '对单/反对单' => 'CHECK_UNCHECK',
        '获取单位价格' => 'GET_UNIT_PRICE',
        '获取可用序列号' => 'GET_AVAILABLE_SERIAL_NUMBERS',
        '批量更新' => 'BATCH_UPDATE',
        '导入' => 'IMPORT',
        '购货单' => 'PURCHASE_ORDER_FORM',
        '购货退货单' => 'PURCHASE_RETURN_ORDER',
        '采购明细表' => 'PURCHASE_DETAILS',
        '采购订单跟踪表' => 'PURCHASE_ORDER_TRACKING',
        '采购汇总表(商品)' => 'PURCHASE_SUMMARY__PRODUCTS_',
        '采购汇总表(供应商)' => 'PURCHASE_SUMMARY__SUPPLIERS_',
        '采购付款一览表' => 'PURCHASE_PAYMENT_OVERVIEW',
        '采购开票管理' => 'PURCHASE_INVOICE_MANAGEMENT',
        '开票' => 'INVOICE',
        '以销定购看板' => 'SALES_BASED_PURCHASE_BOARD',
        '智能补货' => 'SMART_REPLENISHMENT',
        '原始单据' => 'ORIGINAL_DOCUMENTS',
        '选择' => 'SELECT',
        '销货' => 'SALES',
        '销货订单' => 'SALES_ORDER',
        '销货单' => 'SALES_ORDER_FORM',
        '销货退货单' => 'SALES_RETURN_ORDER',
        '销售订单跟踪表' => 'SALES_ORDER_TRACKING',
        '商品销售明细' => 'PRODUCT_SALES_DETAILS',
        '销货开票管理' => 'SALES_INVOICE_MANAGEMENT',
        '销货收款一览表' => 'SALES_COLLECTION_OVERVIEW',
        '销售汇总表(按商品)' => 'SALES_SUMMARY__BY_PRODUCT_',
        '销售汇总表(按客户)' => 'SALES_SUMMARY__BY_CUSTOMER_',
        '销售汇总表(按销售员)' => 'SALES_SUMMARY__BY_SALESPERSON_',
        '销售利润表' => 'SALES_PROFIT_STATEMENT',
        '销售排行表' => 'SALES_RANKING',
        '往来单位欠款表' => 'BUSINESS_UNIT_ARREARS',
        '零售' => 'RETAIL',
        '零售单' => 'RETAIL_ORDER',
        '收银台' => 'CASHIER',
        '收银台商品列表' => 'CASHIER_PRODUCT_LIST',
        '零售退货单' => 'RETAIL_RETURN_ORDER',
        '仓库' => 'WAREHOUSE',
        '调拨单' => 'TRANSFER_ORDER',
        '成本调整单' => 'COST_ADJUSTMENT_ORDER',
        '盘点' => 'INVENTORY_CHECK',
        '导出' => 'EXPORT',
        '序列号盘点' => 'SERIAL_NUMBER_INVENTORY',
        '其他入库单' => 'OTHER_STOCK_IN_ORDER',
        '其他出库单' => 'OTHER_STOCK_OUT_ORDER',
        '组装单' => 'ASSEMBLY_ORDER',
        '拆卸单' => 'DISASSEMBLY_ORDER',
        '库存预警' => 'STOCK_WARNING',
        '商品库存余额表' => 'PRODUCT_STOCK_BALANCE',
        '批次跟踪表' => 'BATCH_TRACKING',
        '商品收发明细表' => 'PRODUCT_RECEIPT_AND_ISSUE_DETAILS',
        '商品收发汇总表' => 'PRODUCT_RECEIPT_AND_ISSUE_SUMMARY',
        '序列号跟踪表' => 'SERIAL_NUMBER_TRACKING',
        '序列号状态表' => 'SERIAL_NUMBER_STATUS',
        '批次保质期清单' => 'BATCH_EXPIRY_LIST',
        '模板列表' => 'TEMPLATE_LIST',
        '获取模板' => 'GET_TEMPLATE',
        '拆卸单模板' => 'DISASSEMBLY_ORDER_TEMPLATE',
        '资金' => 'CAPITAL',
        '付款单' => 'PAYMENT_ORDER',
        '收款单' => 'COLLECTION_ORDER',
        '核销单' => 'WRITE_OFF_ORDER',
        '应收账款明细表' => 'ACCOUNTS_RECEIVABLE_DETAILS',
        '应付账款明细表' => 'ACCOUNTS_PAYABLE_DETAILS',
        '其他收入单' => 'OTHER_INCOME_ORDER',
        '采购销售费用清单' => 'PURCHASE_AND_SALES_EXPENSE_LIST',
        '组合采购销售费用' => 'COMBINE_PURCHASE_AND_SALES_EXPENSES',
        '其他支出单' => 'OTHER_EXPENSE_ORDER',
        '资金转账单' => 'FUND_TRANSFER_ORDER',
        '现金银行报表' => 'CASH_AND_BANK_STATEMENT',
        '客户对账单' => 'CUSTOMER_STATEMENT',
        '供应商对账单' => 'SUPPLIER_STATEMENT',
        '其他收支明细表' => 'OTHER_INCOME_AND_EXPENSE_DETAILS',
        '利润表' => 'PROFIT_STATEMENT',
        '资料' => 'DATA',
        '客户列表' => 'CUSTOMER_LIST',
        '供应商管理' => 'SUPPLIER_MANAGEMENT',
        '供应商列表' => 'SUPPLIER_LIST',
        '供应商分类' => 'SUPPLIER_CATEGORY',
        '账户管理' => 'ACCOUNT_MANAGEMENT',
        '商品管理' => 'PRODUCT_MANAGEMENT',
        '仓库管理' => 'WAREHOUSE_MANAGEMENT',
        '职员管理' => 'STAFF_MANAGEMENT',
        '自取地址管理' => 'PICKUP_ADDRESS_MANAGEMENT',
        '司机管理' => 'DRIVER_MANAGEMENT',
        '辅助资料' => 'AUXILIARY_DATA',
        '客户等级' => 'CUSTOMER_LEVEL',
        '客户分类' => 'CUSTOMER_CATEGORY',
        '商品分类' => 'PRODUCT_CATEGORY',
        '支出类别管理' => 'EXPENSE_CATEGORY_MANAGEMENT',
        '收入类别管理' => 'INCOME_CATEGORY_MANAGEMENT',
        '结算方式' => 'SETTLEMENT_METHOD',
        '单计量单位' => 'SINGLE_UNIT_OF_MEASURE',
        '多计量单位' => 'MULTIPLE_UNITS_OF_MEASURE',
        '商品辅助属性' => 'PRODUCT_AUXILIARY_ATTRIBUTES',
        '客户物料编码' => 'CUSTOMER_MATERIAL_CODE',
        '设置' => 'SETTINGS',
        '系统参数' => 'SYSTEM_PARAMETERS',
        '系统初始化' => 'SYSTEM_INITIALIZATION',
        '初始化' => 'INITIALIZE',
        '结账/反结账' => 'CLOSE_REOPEN_ACCOUNTS',
        '结账' => 'CLOSE_ACCOUNTS',
        '反结账' => 'REOPEN_ACCOUNTS',
        '三方账号管理' => 'THIRD_PARTY_ACCOUNT_MANAGEMENT',
        '订货通' => 'ORDER_SYSTEM',
        '轮播管理' => 'BANNER_MANAGEMENT',
        '订货单管理' => 'ORDER_MANAGEMENT',
        '发货' => 'SHIP',
        '系统配置' => 'SYSTEM_CONFIGURATION',
        '查看/更新配置' => 'VIEW_UPDATE_CONFIGURATION',
        '用户管理' => 'USER_MANAGEMENT',
        '状态切换' => 'STATUS_TOGGLE',
        '下拉列表' => 'DROPDOWN_LIST',
        '查询' => 'QUERY'
    ];

    protected function configure()
    {
        $this->setName('updatemenucomments')
            ->setDescription('Update controller comments with language keys');
    }

    protected function execute(Input $input, Output $output)
    {
        // 获取kuerp模块下的所有控制器文件
        $controllerDir = APP_PATH . 'admin' . DS . 'controller' . DS . 'kuerp';
        $this->updateControllers($controllerDir, $output);
        
        $output->writeln("Controller comments updated successfully!");
    }

    protected function updateControllers($dir, $output)
    {
        $files = scandir($dir);
        foreach ($files as $file) {
            if ($file === '.' || $file === '..') {
                continue;
            }
            
            $filePath = $dir . DS . $file;
            if (is_dir($filePath)) {
                // 递归处理子目录
                $this->updateControllers($filePath, $output);
            } elseif (pathinfo($file, PATHINFO_EXTENSION) === 'php') {
                // 处理PHP文件
                $this->updateControllerFile($filePath, $output);
            }
        }
    }

    protected function updateControllerFile($filePath, $output)
    {
        $content = file_get_contents($filePath);
        
        // 检查是否有需要替换的中文标题
        $updated = false;
        foreach ($this->menuLangKeys as $chineseTitle => $langKey) {
            // 查找控制器类注释中的中文标题
            $pattern = '/(\s*\*\s*)' . preg_quote($chineseTitle, '/') . '(\s*\*)/u';
            if (preg_match($pattern, $content)) {
                // 替换为语言键
                $replacement = '$1{:' . "__('" . $langKey . "')" . '}$2';
                $content = preg_replace($pattern, $replacement, $content);
                $updated = true;
                $output->writeln("Updated {$filePath}: {$chineseTitle} -> {$langKey}");
            }
        }
        
        // 如果有更新，写回文件
        if ($updated) {
            file_put_contents($filePath, $content);
        }
    }
}